package com.client;

import com.config.JksConfiguration;
import com.utils.CertUtil;
import com.utils.LogUtil;

import java.io.File;
import java.security.PrivateKey;
import java.util.List;

/**
 * 使用私钥{@code jks }来初始化RepChainClient
 *
 * @author zyf
 */
public class RepChainJksClient extends RepChainClient {

    private JksConfiguration jksConfig;

    public RepChainJksClient() {
    }

    /**
     * @param host 服务主机地址
     */
    public RepChainJksClient(String host) {
        super(host);
    }

    /**
     * 使用JksConfiguration初始化JksClient
     *
     * @param jksConfig
     */
    public RepChainJksClient(JksConfiguration jksConfig) {
        this.jksConfig = jksConfig;
        this.host = jksConfig.getRcJavaConfig().getString("client.host");
        try {
            initialJks(jksConfig);
        } catch (Exception e) {
            logMsg(LogType.Error, e.getMessage());
            e.printStackTrace();
        }
        logMsg(LogType.Info, "使用client.host初始化，host：{}", host);
    }

    /**
     * 使用jks私钥初始化Client
     *
     * @param host     服务主机地址
     * @param jksPath  jks路径
     * @param password jks密码
     * @param alias    别名
     */
    public RepChainJksClient(String host, String jksPath, String password, String alias) {
        this.host = host;
        try {
            initialJks(jksPath, password, alias);
        } catch (Exception e) {
            e.printStackTrace();
            logMsg(LogUtil.LogType.Error, e.getMessage());
        }
    }

    /**
     * 设置jks
     *
     * @param jksPath  jks文件路径
     * @param password jks密码
     * @param alias    别名
     */
    public void setJks(String jksPath, String password, String alias) {
        try {
            initialJks(jksPath, password, alias);
        } catch (Exception e) {
            logMsg(LogType.Error, e.getMessage());
            e.printStackTrace();
        }
    }


    /**
     * 通过jksConfig设置jks
     *
     * @param jksConfig
     */
    public void setJksConfig(JksConfiguration jksConfig) {
        try {
            initialJks(jksConfig);
        } catch (Exception e) {
            logMsg(LogType.Error, e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 初始化jksClient客户端
     *
     * @param jksPath
     * @param password
     * @param alias
     * @throws Exception
     */
    private void initialJks(String jksPath, String password, String alias) throws Exception {
        List list = CertUtil.getCertFromJKS(new File(jksPath), password, alias);
        if (list == null) {
            throw new RuntimeException("读取Jks失败");
        }
        this.privateKey = (PrivateKey) list.get(1);
        this.jksInit = true;
        logMsg(LogType.Info, "RepChainClient使用JKS初始化成功");
    }

    /**
     * 初始化jksClient客户端
     *
     * @param jksConfig
     * @throws Exception
     */
    private void initialJks(JksConfiguration jksConfig) throws Exception {
        List list = CertUtil.getCertFromJKS(jksConfig);
        if (list == null) {
            throw new RuntimeException("读取Jks失败");
        }
        this.jksConfig = jksConfig;
        this.privateKey = (PrivateKey) list.get(1);
        this.jksInit = true;
        logMsg(LogType.Info, "RepChainClient使用JKS初始化成功");
    }
}
